package com.itheima.servlet;

import com.itheima.mapper.StuMapper;
import com.itheima.service.StuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    StuService stuService = new StuService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取浏览器端传过来的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //处理逻辑
        boolean flag = false;
        if (stuService.checkExists(username,password)){
            flag = true;
        }
        //响应给浏览器
        //设置响应格式,浏览器收到响应体按照html格式进行解析,响应体编码格式
        //按照utf8进行解码
        resp.setContentType("text/html;charset=utf-8");
        if (flag){
            resp.getWriter().write("欢迎登录:"+ username);
        }else {
            resp.getWriter().write("用户密码错误,请重新登录:"
                    +"<a href='/HM/login.html'>重新登陆</a>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
